<template>
    <div style="box-sizing: border-box;padding: 15px;background-color: white;">

        <div style="margin-bottom: 15px;">
            <el-button type="primary" @click="handleOpenGenerateConfigDialogClick">修改生成配置</el-button>
            <el-button type="success" @click="handleGenerateClick">一键生成</el-button>
        </div>

        <el-table
            ref="tableRef"
            v-loading="tableLoading"
            border style="width: 100%"
            :data="tableData"
            element-loading-text="加载中..."
            element-loading-svg-view-box="-10, -10, 50, 50"
            element-loading-background="rgba(122, 122, 122, 0.3)"
            @selection-change="handleTableSelectionChange">
            <el-table-column type="selection" width="80"/>
            <el-table-column prop="tableName" label="表名"/>
        </el-table>

        <GenerateConfigDialog ref="generateConfigDialogRef" v-model:show="generateConfigDialog.show"/>
    </div>
</template>

<script setup>
import {ref, toRaw} from 'vue'
import GenerateConfigDialog from "./GenerateConfigDialog.vue";
import {doCodeGenerate, queryTables} from "../../request/index.js";

const generateConfigDialog = ref({
    show: false,
})

const tableLoading = ref(false)

const tableRef = ref()

const generateConfigDialogRef = ref()

const tableData = ref([])

// 打开配置的弹窗
const handleOpenGenerateConfigDialogClick = () => {
    generateConfigDialog.value.show = true
}

const handleTableSelectionChange = () => {

}

// 点击一键生成按钮
const handleGenerateClick = () => {
    let rows = tableRef.value.getSelectionRows()
    if (rows.length === 0) {
        ElMessage.warning('没有选择任何行')
        return
    }
    // 表信息
    let tables = []
    rows.forEach(item => {
        tables.push({
            tableName: item.tableName
        })
    })
    console.log(generateConfigDialogRef.value.geneGlobalConfig)
    let data = {
        createTableConfigList: tables,
        geneGlobalConfig: toRaw(generateConfigDialogRef.value.geneGlobalConfig),
        geneModelConfig: toRaw(generateConfigDialogRef.value.geneModelConfig)
    }
    const loadingInstance = ElLoading.service({
        text: '加载中...'
    })
    doCodeGenerate(data).then(res => {
        ElMessage.success('生成成功!')
    }).catch(err => {
        console.log('err:', err)
    }).finally(() => {
        loadingInstance.close()
    })
}

const init = () => {
    tableLoading.value = true
    queryTables().then(res => {
        tableData.value = res.data
    }).catch(err => {
        console.log('查询异常：', err)
    }).finally(() => {
        tableLoading.value = false
    })
}

init()
</script>

<style scoped lang="less">

</style>
